package cn.itcast.travel.service.impl;

import cn.itcast.travel.dao.CommentDao;
import cn.itcast.travel.dao.impl.CommentDaoImpl;
import cn.itcast.travel.domain.Comment;
import cn.itcast.travel.service.CommentService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentServiceImpl implements CommentService {
    private CommentDao cd = new CommentDaoImpl();
    /**
     * 给线路添加评论
     * @param rid 线路id
     * @param uid 用户id
     * @param content 评论内容
     */
    @Override
    public void addCommit(String rid, int uid, String content) {
        Comment c = new Comment();
        c.setRid(Integer.parseInt(rid));
        c.setUid(uid);
        c.setTime(new Date());
        c.setContent(content);
        System.out.println("comm:"+c);
        cd.addComment(c);
    }

    /**
     * 获取线路评论
     * @param rid
     * @return
     */
    @Override
    public List<cn.itcast.travel.domain.vo.Comment> getCommit(String rid) {
        //根据线路id获取线路的评论
        List<Comment> commentByRid = cd.getCommentByRid(Integer.parseInt(rid));
        List<cn.itcast.travel.domain.vo.Comment> resuleLise = new ArrayList<>();
        //遍历查询到的评论集合封装到pl的VO集合里面
        for (Comment comment : commentByRid) {
            try {
                cn.itcast.travel.domain.vo.Comment clone = comment.clone(cn.itcast.travel.domain.vo.Comment.class);
                clone.setText(comment.getContent());
                resuleLise.add(clone);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resuleLise;
    }
}
